﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.Caches;
using Enterprise.Management.Business;
using Enterprise.Management.FilterAttributes;
using Enterprise.Management.ViewModels;

namespace Enterprise.Management.Controllers
{
    [CategoryFunction]
    public class DependencyEconomyController : BaseController
    {
        DependencyEconomyBLL bllDependencyEconomy = new DependencyEconomyBLL();

        public ActionResult Index()
        {
            DependencyEconomyViewModel vmDependencyEconomy = new DependencyEconomyViewModel();
            DP_Economy_Info dp_economy_info = bllDependencyEconomy.Get(BranchCategoryCache.BranchCategoryId.Value);
            if (dp_economy_info != null)
            {
                vmDependencyEconomy.isNew = false;
                vmDependencyEconomy.dp_economy_info = dp_economy_info;
            }
            else
            {
                vmDependencyEconomy.isNew = true;
            }
            return View(vmDependencyEconomy);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(EditDependencyEconomyViewModel vmDependencyEconomy)
        {
            if (ModelState.IsValid)
            {
                DP_Economy_Info dp_economy_info = new DP_Economy_Info();
                dp_economy_info.EcnInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_economy_info.EcnInf_EcnDevStatus = vmDependencyEconomy.EcnInf_EcnDevStatus;
                dp_economy_info.EcnInf_Population = vmDependencyEconomy.EcnInf_Population;
                dp_economy_info.EcnInf_Potential = vmDependencyEconomy.EcnInf_Potential;
                dp_economy_info.EcnInf_CmptStatus = vmDependencyEconomy.EcnInf_CmptStatus;
                dp_economy_info.EcnInf_LclDevStatus = vmDependencyEconomy.EcnInf_LclDevStatus;
                dp_economy_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_economy_info.ModifiedDate = DateTime.Now;

                if (bllDependencyEconomy.Add(dp_economy_info))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "新建失败，请稍后再试！");
                }
            }
            return View(vmDependencyEconomy);
        }

        public ActionResult Edit()
        {
            EditDependencyEconomyViewModel vmDependencyEconomy = new EditDependencyEconomyViewModel();
            DP_Economy_Info dp_economy_info = bllDependencyEconomy.Get(BranchCategoryCache.BranchCategoryId.Value);
            vmDependencyEconomy.EcnInf_EcnDevStatus = dp_economy_info.EcnInf_EcnDevStatus;
            vmDependencyEconomy.EcnInf_Population = dp_economy_info.EcnInf_Population;
            vmDependencyEconomy.EcnInf_Potential = dp_economy_info.EcnInf_Potential;
            vmDependencyEconomy.EcnInf_CmptStatus = dp_economy_info.EcnInf_CmptStatus;
            vmDependencyEconomy.EcnInf_LclDevStatus = dp_economy_info.EcnInf_LclDevStatus;
            return View(vmDependencyEconomy);
        }

        [HttpPost]
        public ActionResult Edit(EditDependencyEconomyViewModel vmDependencyEconomy)
        {
            if (ModelState.IsValid)
            {
                DP_Economy_Info dp_economy_info = bllDependencyEconomy.Get(BranchCategoryCache.BranchCategoryId.Value);
                dp_economy_info.EcnInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_economy_info.EcnInf_EcnDevStatus = vmDependencyEconomy.EcnInf_EcnDevStatus;
                dp_economy_info.EcnInf_Population = vmDependencyEconomy.EcnInf_Population;
                dp_economy_info.EcnInf_Potential = vmDependencyEconomy.EcnInf_Potential;
                dp_economy_info.EcnInf_CmptStatus = vmDependencyEconomy.EcnInf_CmptStatus;
                dp_economy_info.EcnInf_LclDevStatus = vmDependencyEconomy.EcnInf_LclDevStatus;
                dp_economy_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_economy_info.ModifiedDate = DateTime.Now;

                if (bllDependencyEconomy.Update(dp_economy_info))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "修改失败，请稍后再试！");
                }
            }

            return View(vmDependencyEconomy);
        }
    }
}
